/**
 * 
 */
package net.toocruel.iqismart.dao.spec;

import net.toocruel.iqismart.dao.support.BaseSpecification;
import net.toocruel.iqismart.dao.support.QueryWraper;
import net.toocruel.iqismart.entity.rbac.Role;
import net.toocruel.iqismart.dao.condition.RoleCondition;
import org.apache.commons.lang.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;

/**
 * @author toocruel
 *
 */
public class RoleSpec extends BaseSpecification<Role, RoleCondition> {

	public RoleSpec(RoleCondition condition) {
		super(condition);
	}

	@Override
	protected void addCondition(QueryWraper<Role> queryWraper) {
		CriteriaBuilder cb = queryWraper.getCb();
		Root<Role> root = queryWraper.getRoot();

		if(getCondition().getCreatedAtStart() != null && getCondition().getCreatedAtEnd() != null){
			queryWraper.addPredicate(cb.greaterThanOrEqualTo(root.get("createdAt"),getCondition().getCreatedAtStart()));
			queryWraper.addPredicate(cb.lessThanOrEqualTo(root.get("createdAt"),getCondition().getCreatedAtEnd()));
		}

		if(StringUtils.isNotBlank(getCondition().getId())){
			queryWraper.addPredicate(cb.equal(root.get("id"),getCondition().getId()));
		}
		if(StringUtils.isNotBlank(getCondition().getName())){
			queryWraper.addPredicate(cb.equal(root.get("name"),getCondition().getName()));
		}

	}

	@Override
	protected void addFetch(Root<Role> root) {
		//root.fetch("roles");
	}
}
